package com.cloudxingkong.dianyinke.NewTeacher.Fragment;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.cloudxingkong.dianyinke.NewTeacher.Adapter.PingJiaAdapter;
import com.cloudxingkong.dianyinke.NewTeacher.Utils.CookieHelper;
import com.cloudxingkong.dianyinke.NewTeacher.bean.StuEvat;
import com.cloudxingkong.dianyinke.R;
import com.cloudxingkong.dianyinke.Uri.teacherUri.ThisUri;
import com.google.gson.Gson;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 云星空科技 on 2016/3/18.
 */
public class PingJiaFragment extends BaseFragment {

    private ListView listview;
    private List<StuEvat.ListBean> datas;

    private SharedPreferences sharedPreferences;
    private String sessionID;
    private ProgressBar progressBar;
    private TextView tvMsg;
    private PingJiaAdapter adapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_pingjia, null);
        listview = (ListView) view.findViewById(R.id.pinjia_listview);
        datas = new ArrayList<>();
        adapter = new PingJiaAdapter(datas, getActivity());
        listview.setAdapter(adapter);
        progressBar = (ProgressBar) view.findViewById(R.id.pb_pingjia);
        tvMsg = ((TextView) view.findViewById(R.id.pingjia_tv_msg));
        sharedPreferences = getActivity().getSharedPreferences("data_dyk", 0);
        sessionID = CookieHelper.getSessionID(sharedPreferences);
        loadPingjia();

        return view;
    }

    private void loadPingjia() {
        progressBar.setVisibility(View.VISIBLE);
        RequestParams params = new RequestParams(ThisUri.SERVER_YUMING + ThisUri.LESSON_EVAT);
        params.addHeader("cookie", sessionID);
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Log.e("test", "学生评价 " + result);
                Message msg = handler.obtainMessage();
                msg.what = 1;
                msg.obj = result;
                handler.sendMessage(msg);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                progressBar.setVisibility(View.GONE);
                handler.sendEmptyMessage(2);
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });

    }


    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    String result = (String) msg.obj;
                    Gson gson = new Gson();
                    if (result != null && !result.equals("")) {
                        progressBar.setVisibility(View.GONE);
                        StuEvat evat = gson.fromJson(result, StuEvat.class);
                        if (evat.getList() != null && evat.getList().size() > 0) {
                            datas.addAll(evat.getList());
                            adapter.notifyDataSetChanged();
                        } else {
                            Toast.makeText(getActivity(), "获取评价失败", Toast.LENGTH_SHORT).show();
                        }
                    } else {
                        progressBar.setVisibility(View.GONE);
                        tvMsg.setVisibility(View.VISIBLE);
                    }
                    break;
                case 2:
                    Toast.makeText(getActivity(), "服务器出了点小差，请稍后重试", Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };
}
